/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jastrzab.flashlight;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import org.jastrzab.core.CameraContant;
import org.jastrzab.core.CameraWindow;

/**
 *
 * @author Jastrząb
 */
public final class Light extends Service implements CameraContant {

	private static boolean mWorking = false;
	private boolean mSwitch = false;
	private CameraWindow mCamera;

	public static boolean isWorking() {
		return mWorking;
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		mWorking = true;
		mCamera = new CameraWindow(Light.this);
		mCamera.baginProcesing();
		notifyWidget();
		Log.d(LOG, "Light Service Start");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		mCamera.endProcesing();
		mSwitch = false;
		mWorking = false;
		notifyWidget();
		Log.d(LOG, "Light Service Stop");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (intent != null && intent.getAction() != null) {
			if (intent.getAction().equals(Intents.WIDGET)) {
				if (mSwitch) {
					stopSelf();
				} else {
					mSwitch = true;
				}
			}
		}
		return super.onStartCommand(intent, flags, startId);
	}

	private void notifyWidget() {
		sendBroadcast(Intents.getWidget(Light.this));
	}

}
